//
//  HEGoodsDetailsHtmlCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsDetailsHtmlCell.h"
#import <RZColorful/RZColorful.h>

@interface HEGoodsDetailsHtmlCell ()<WKNavigationDelegate>
 

@property (nonatomic, strong) UILabel *titleLb;

@property (nonatomic, strong) UITextView *contentLb;
@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, assign) BOOL hasLoad;

@end

@implementation HEGoodsDetailsHtmlCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
     // Initialization code
    
    _titleLb = [UILabel new];
    _titleLb.text = @"商品详情";
    _titleLb.font = DYBlodFont(18);
    [self.contentView addSubview:_titleLb];
    
    _contentLb = [UITextView new];
    _contentLb.editable=NO;
    _contentLb.scrollEnabled = NO;
    _contentLb.font = DYFont(15);
    [self.contentView addSubview:_contentLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];

    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
    }];
    [_contentLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

        
    //js脚本 （脚本注入设置网页样式）
//    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
//
//    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width,user-scalable=no'); document.getElementsByTagName('head')[0].appendChild(meta);";
//
//    //注入
//    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
//    [wkUController addUserScript:wkUScript];
//    //配置对象
//    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
//    wkWebConfig.userContentController = wkUController;
//    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 300) configuration:wkWebConfig];
//    [self.contentView addSubview:_webView];
//    _webView.scrollView.bounces = NO;
//    _webView.scrollView.scrollEnabled = NO;
//    _webView.navigationDelegate = self;
//    _webView.backgroundColor = UIColor.orangeColor;
//
//    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
//         make.top.equalTo(_titleLb.mas_bottom).mas_offset(15);
//         make.left.equalTo(self.contentView).mas_offset(15);
//         make.right.equalTo(self.contentView).mas_offset(-15);
//         make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
//     }];
 
}


- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath{
    _contentLb.attributedText = formModel.goodsModel.KdetailMobileHtml;
    
//    if (_hasLoad) {
//         return;
//     }
//    
//    if (formModel.goodsModel) {
//        _hasLoad = true;
//    }
//    [_webView loadHTMLString:formModel.goodsModel.detailMobileHtml baseURL:nil];
}



#pragma mark - WKNavigationDelegate
 
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"加载完成");
   //这个方法也可以计算出webView滚动视图滚动的高度
   [webView evaluateJavaScript:@"document.body.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
        NSLog(@"scrollHeight高度：%.2f",[result floatValue]);
        [self.webView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset([result floatValue]);
        }];
    }];
}

#pragma mark  - KVO回调
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
//
//    //更具内容的高重置webView视图的高度
//    CGFloat newHeight = self.wbScrollView.contentSize.height;
//    NSLog(@"kvo算出的高度啊：%.f",newHeight);
//    [self resetWebViewFrameWithHeight:newHeight];
//
//}

 

@end
